package com.xyhuo.leetcode.december;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 整数转罗马数字
 * https://leetcode.cn/problems/integer-to-roman/
 *
 * @time 2023/12/1
 */
public class IntToRoman {

//    public static void main(String[] args) {
//        IntToRoman intToRoman = new IntToRoman();
//        intToRoman.intToRoman(58);
//    }

    public String intToRoman(int num) {
        Queue<Integer> queue = new LinkedList<>();
        int tmp = num;
        while (true) {
            if (tmp < 10) {
                queue.add(tmp);
                break;
            }
            queue.add(tmp % 10);
            tmp = tmp / 10;
        }
        StringBuilder numSB = new StringBuilder();
        int i = 0;
        char[] chars = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        while (!queue.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            int pop = queue.remove();
            if (pop < 4) {
                for (int j = 0; j < pop; j++) {
                    sb.append(chars[i]);
                }
            } else if (pop == 4) {
                sb.append(chars[i]);
                sb.append(chars[i + 1]);
            } else if (pop == 5) {
                sb.append(chars[i + 1]);
            } else if (pop < 9) {
                sb.append(chars[i + 1]);
                for (int k = 5; k < pop; k++) {
                    sb.append(chars[i]);
                }
            } else if (pop == 9) {
                sb.append(chars[i]);
                sb.append(chars[i + 2]);
            }
            numSB.insert(0, sb);
            i += 2;
        }
        return numSB.toString();
    }
}
